var resourceUrl = getApp().request("imgUrl");
var requestUrl = getApp().request("url");
var app = getApp().globalData;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    btnShow:0,
    contentH:"",
    isBuyBox: {
      isShow:false,
      infor:""
    },
    bugGiftSuccess: false,
    giftBoxInfor: [["初级礼盒",5000], ["中级礼盒",10000],["高级礼盒",30000]],
    resourceUrl: resourceUrl
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },
  //点击立即购买
  btnNowBuyGiftBox:function(e){
    var that=this;
    console.log(e.currentTarget.dataset.index)
    var index=e.currentTarget.dataset.index;
    this.setData({
      "isBuyBox.isShow": true,
      "isBuyBox.infor":{
        name: that.data.giftBoxInfor[index][0],
        price: that.data.giftBoxInfor[index][1],
        imgUrl:index
      }
    })
  },
  //顶部按钮切换
  btnSwitch: function (e) {
    console.log(111)
    this.setData({
      btnShow: e.currentTarget.dataset.btn
    })
  },
  //弹出的购买框取消
  btnCancelBuy() {
    this.setData({
      "isBuyBox.isShow": false
    })
  },
  //弹出的购买框确定
  btnensureBuy(e) {
    var gold = app.gold;
    var singlePrice= this.data.isBuyBox.infor.price;
    console.log(singlePrice)
    if (gold < singlePrice){
      getApp().showModal('您的金币数量不足');
      return;
    }
    var that = this;
    var giftBoxType = that.data.isBuyBox.infor.imgUrl;     //礼盒类型
    wx.showLoading({
      title: '',
      mask: true
    })
    wx.request({
      url: requestUrl+'buyGiftBox',
      method: "POST",
      data: { userId: app.session_key.userId, salt: app.session_key.salt, "type": (giftBoxType+1)},
      header: getApp().request("header") ,
      success:function(res){
         console.log(res)
         if (res.statusCode != 200) {
           getApp().showModal("服务器异常");
           return
         }
         wx.hideLoading();
         if (res.code=200){
           that.setData({
             "isBuyBox.isShow": false,
             bugGiftSuccess:true
           });
           //买礼盒消耗金币，本地存储相应改变
           gold -= singlePrice
           app.gold=gold;
           //买到礼盒，本地存储礼盒相应改变
           var giftBoxTypeName=null;
           switch (giftBoxType){
             case 0:
               giftBoxTypeName ="juniorGiftBoxN";
               break;
             case 1:
               giftBoxTypeName = "middleGiftBoxN";
               break;
             case 2:
               giftBoxTypeName = "seniorGiftBoxN";  
               break;  
           }
           var giftBox = app[giftBoxTypeName];
           giftBox++;
           app[giftBoxTypeName]=giftBox;
           console.log(app)
         } else if (res.code == 401 || res.code == 402 || res.code == 301 || res.code == 302){
           console.log(res.code)
           getApp().showModal("提交信息有误！");
         } else if (res.code == 303) {
           getApp().showModal("您的金币不足！");
         } else if (res.data.code == 800) {
           getApp().showModal(res.data.data.msg);
           wx.hideLoading();
         }
      },
      fail: (fai) => {
        console.log("点击确定购买礼盒fail");
        console.log(fai)
        wx.hideLoading();
        getApp().showModal("网络超时，请检查您的网络");
      }
    })
    
  },
  //点击立即打开
  btnNowOpen(){
    wx.navigateTo({
      url: '../warehouse/warehouse',
    })
  },
  //点击叉关闭
  btnClose() {
    this.setData({
      bugGiftSuccess: false
    });
  },
  onShareAppMessage: function () {
    return {
      title: getApp().shareTitle(),
      path: "/pages/index/index"
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      bugGiftSuccess: false
    });
    wx.showShareMenu({
      withShareTicket: true
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage:function(){
    //是否选车
    var fightCarTypeId = app.fightCarTypeId;
    //随机一句宣言
    var declaration = getApp().Refit();
    var myRefitTxt = Math.floor(Math.random() * declaration.length);
    
    if (fightCarTypeId == 0) {
      return {
        title: getApp().shareTitle1(),
        path: "/pages/index/index"
      }
    } else {
      return {
        title: getApp().shareTitle2(),
        path: "/pages/openShare/openShare?shareId=" + app.session_key.userId + "&carTypeId=" + fightCarTypeId + "&shareRefitNum=" + myRefitTxt + "&carName=SUV概念车",
        success: (re) => {
          if (Object.keys(re).length == 1) {
            return
          }
          var ticket = re.shareTickets[0];
          getApp().shareFn(ticket);
        }
      }
    }
  }
})